<?php

/**
*
* User - Action controller
*
* Initially by Derek 
*
* Added flash data, & authenication error handler
*
* George Dunlop
*
* September 1, 2006 added load language file
* to CI standard language implementation.
* Thomas Traub tomcode.com
*/

class User extends Controller {

	function User()
	{
		parent::Controller();
		$this->lang->load('userauth', $this->session->userdata('ua_language'));
	}
	
	function index()
	{
		// should not be a link to here so return back
		redirect ($this->session->flashdata('uri'));
		// change this to 
		// if login -> edit profile
		// if ! login -> login page
	}
	
	function login()
	{
		$username = $this->input->post('username');
		$password = $this->input->post('password');
		$remember_me = $this->input->post('remember_me');

		// check if userauth db current or needs to be installed 
		if ($this->userauth->valid_db()) { 
			// valid db, try login
			if($username && $password) {
      
				if($this->userauth->trylogin($username, $password, TRUE)) {
					if ($remember_me) { 
						$this->remember_me->addRememberMe($username); 
					}
					// return page is based on flash data
					redirect ($this->session->flashdata('uri'));
				} else {
					$data['content'] = '<p class="error">' 
						. $this->lang->line('ua_log_error').'</p>';
				}
			}else {
					$data['content'] = '<p class="error">'
						. $this->lang->line('ua_name_and_pswd').'</p>';
			}
			$data['title'] = $data['h1_title'] = $this->lang->line('ua_login_error');

			// Display in Template
			$this->load->view('template', $data);

		} else { 

			// need to install/upgrade db
			redirect ('install'); 
		}
	}

	function logout()
	{
		$this->userauth->logout();

		// Two choices here, comment out one or the other
		redirect ($this->session->flashdata('uri'));
//		$this->_display_logout_message();
		// change this to ua_config option 
		// for redirect to /user with logout message
	}

	// private function to display a logout message
	function _display_logout_message() {
		$this->session->keep_flashdata('uri');
		$data['title'] = $data['h1_title'] = $this->lang->line('ua_logout_title');
		$data['content'] = '<p>'.$this->lang->line('ua_logout_txt').'</p>';

		// Display in Template
		$this->load->view('template', $data);
	}

	function auth_error()
	{
		$this->session->keep_flashdata('uri');

		$data['title'] = $data['h1_title'] = $this->lang->line('ua_auth_err_title');
		if($this->userauth->loggedin()) {
			$data['content'] = '<p>'.$this->lang->line('ua_auth_denied').'</p>';
		} else { 
			$data['content'] = '<p>'.$this->lang->line('ua_auth_not_logged').'</p>';
		}

		// Display in Template
		$this->load->view('template', $data);
	}

	// switch session's language and set a cookie
	function set_language() {
		$language = $this->uri->segment(3);
		if ( empty($language) ) { $language = $this->input->post('lang_select'); }
		if ( $language == 'detect' ) {
			// kill the cookie & session variable
			set_cookie('ua_language', '');
			$this->session->set_userdata('ua_language', '');
		} else {
			// switch session's language and set a cookie
			set_cookie('ua_language', $language, 
				$this->config->item('remember_me_life'));
			$this->session->set_userdata('ua_language', $language);
		}
		redirect ($this->session->flashdata('uri'));
	}
}

?>